import { defineStore } from "pinia"
import type { PersistenceOptions } from "pinia-plugin-persistedstate"
import { reactive, ref } from "vue"
import type { UserInfo } from "../types/types";




export const userStore = defineStore("userStore", () => {

    const userLoginStatus = ref<boolean>(false);

    // 存储用户信息
    const userInfo = reactive<UserInfo>({
        username: "",
        gex: "",
        uu_id: "",
        token: "",
        avatar: "",
        birthday: "",
        country: "",
        province: "",
        city: "",
        TypeOfFriendShips: [],
        signatureOfTheIndividual: ""
    })

    // 设置用户信息
    function setStatus() {
        userLoginStatus.value = !userLoginStatus.value;
    }
    function setUserInfo(user: UserInfo) {
        userInfo.username = user.username;
        userInfo.gex = user.gex;
        userInfo.birthday = user.birthday;
        userInfo.country = user.country;
        userInfo.province = user.province;
        userInfo.city = user.city;
        userInfo.uu_id = user.uu_id;
        userInfo.avatar = user.avatar;
        userInfo.signatureOfTheIndividual = user.signatureOfTheIndividual;
    }
    function setToken(token: string) {
        userInfo.token = token;
    }
    function clearUserInfo() {
        userInfo.username = "";
        userInfo.gex = "";
        userInfo.birthday = "";
        userInfo.country = "";
        userInfo.province = "";
        userInfo.city = "";
        userInfo.uu_id = "";
        userInfo.avatar = "";
        userInfo.signatureOfTheIndividual = "";
    }


    return {
        userLoginStatus,
        userInfo,
        setUserInfo,
        setStatus,
        clearUserInfo,
        setToken
    }


}, {
    persist: {
        key: "userStore",
        paths: ['userLoginStatus'],
        storage: localStorage
    } as PersistenceOptions
});